package net.ib.mtalk.ui.setting.user;

import net.ib.mntalk.protocol.Common.AccountAuthToken;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenRes;
import net.ib.mtalk.R;
import net.ib.mtalk.constant.HandlerMsgConst;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EmailActivity extends Activity 
{
	private EditText mEditText;
	private Button mButton;
	
	private Context mContext;
	
	private EmailController mUiController;
	
	private ProgressDialog mProgressDialog;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_SUCCESS", Toast.LENGTH_SHORT).show();
				
				AcquireAccountAuthTokenRes res = (AcquireAccountAuthTokenRes) msg.obj;
				AccountAuthToken accountAuthToken = res.getAccountAuthToken();
				
				mUiController.acquireAccountToServer(accountAuthToken);
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_AUTH_FAIL", Toast.LENGTH_SHORT).show();
				mProgressDialog.hide();
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_SUCCESS:
				mUiController.setUserAccount((AccountToken)msg.obj);
				break;
			case HandlerMsgConst.HANDLER_ACQUIRE_ACCOUNT_FAIL:
				Toast.makeText(mContext, "HANDLER_ACQUIRE_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				mProgressDialog.hide();
				break;
			case HandlerMsgConst.HANDLER_REGISTER_ACCOUNT_SUCCESS:
				Toast.makeText(mContext, "HANDLER_REGISTER_ACCOUNT_SUCCESS", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case HandlerMsgConst.HANDLER_REGISTER_ACCOUNT_FAIL:
				Toast.makeText(mContext, "HANDLER_REGISTER_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				mProgressDialog.hide();
				break;
			case HandlerMsgConst.HANDLER_UNKNOWN_ERROR:
				Toast.makeText(mContext, "HANDLER_UNKNOWN_ERROR", Toast.LENGTH_SHORT).show();
				mProgressDialog.hide();
				break;
			default:
				break;
			}
		}
	};
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_email);
		mContext = this;
		
		mUiController = new EmailController(mContext, mHandler);
		
		mEditText = (EditText)findViewById(R.id.email_id);
		mButton = (Button)findViewById(R.id.regist_btn);
		
		mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setMessage(getString(R.string.waiting));
		mProgressDialog.setCancelable(false);
		
		mButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String id = mEditText.getText().toString();
				if(id.trim().length() < 1)
				{
					new AlertDialog.Builder(mContext).setMessage(getString(R.string.setting_profile_email_wrong_msg))
					.setNegativeButton(R.string.done, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).create().show();
				}
				else if(isExistAccount(id))
				{
					AlertDialog dialog = new AlertDialog.Builder(mContext).setMessage(getString(R.string.setting_profile_add_account_exist_msg))
					.setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).create();
					dialog.setCancelable(false);
					dialog.show();
				}
				else
				{
					mUiController.acquireAccountAuthToServer(id.trim());
					mProgressDialog.show();
				}
			}
		});
	}
	
	private boolean isExistAccount(String account)
	{
		return getIntent().getStringArrayListExtra("email").contains(account);
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		if(mProgressDialog != null)
		{
			mProgressDialog.hide();
			mProgressDialog.dismiss();
		}
	}
}
